/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.caracol.db.AccesoDatos;
import com.caracol.db.CaracolQueryGenerator;
import java.sql.SQLException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Roquet
 */
public class Arresto {
    
    /**
     * Name of the table in the database
     */
    private static final String TABLE = "arrestos";
    
    /**
     * Name of the id column in the database;
     */
    private static final String ID = "id";
    
    /**
     * Name of the fecha column in the database
     */
    private static final String FECHA = "fecha";
    
    /**
     * Name of the causa column in the database
     */
    private static final String CAUSA = "causa";
    
    /**
     * Name of the horas column in the database
     */
    private static final String HORAS = "horas";
    
    /**
     * Name of the id oficial column in the database
     */
    private static final String ID_OFICIAL = "id_oficial";
    
    private int id;
    private Date fecha;
    private String causa;
    private int horas;
    private int idOficial;
    
    private static CaracolQueryGenerator queryGenerator = new CaracolQueryGenerator();
    
    private Arresto(int id, Date fecha, String causa, int horas, int idOficial)
    {
        this.id = id;
        this.fecha = fecha;
        this.causa = causa;
        this.horas = horas;
        this.idOficial = idOficial;
    }
    
    public Arresto()
    {
        this(0, new Date(), "", 0, 0);
    }
    
    public Arresto(Date fecha, String causa, int horas, int idOficial)
    {
        this(0, fecha, causa, horas, idOficial);
    }
    
    public static Arresto getArresto(int id) throws SQLException, Exception
    {
        Arresto arresto = null;
        
        String[] colums = new String[] { ID, FECHA, CAUSA, HORAS, ID_OFICIAL };
        Map<String, String> where = new LinkedHashMap<String, String>();
        where.put(ID, new Integer(id).toString());
        
        List<Object> rawArresto = queryGenerator.query(colums, TABLE, where, null, null, null, null);
        if (rawArresto != null && rawArresto.size() > 0) {
            List<Object> row = (List<Object>) rawArresto.get(0);
            arresto = new Arresto();
            arresto.id = id;
            arresto.setFecha((Date) row.get(1));
            arresto.setCausa(row.get(2).toString());
            arresto.setHoras(Integer.parseInt(row.get(3).toString()));
            arresto.setIdOficial(Integer.parseInt(row.get(4).toString()));
        }
        
        return arresto;
    }
    
      /**
     * Get's all the arresto entitys in found in the database table
     * @return <code>List</code> containing all the rows in the table of the database
     * @throws SQLException if some database exception happend during the execution of this operation
     * @throws Exception if some exception happend during the execution of this operation
     */
    public static List<Arresto> getAll() throws SQLException, Exception{
        ArrayList<Arresto> arrestos = new ArrayList<Arresto>();
        String[] columns = new String[] { ID, FECHA, CAUSA, HORAS, ID_OFICIAL };
        List<Object> rawCargo = queryGenerator.query(columns, TABLE, null, null, null, null, null);
        
        for (Object object : rawCargo) {
            List<Object> row = (List<Object>) object;
            if (row != null && row.size() > 0) {
                List<Object> rawArma = (List<Object>) row.get(0);
                Arresto arresto = new Arresto();
                arresto.id = Integer.parseInt(rawArma.get(0).toString());
                arresto.setFecha((Date) rawArma.get(1));
                arresto.setCausa(rawArma.get(2).toString());
                arresto.setHoras(Integer.parseInt(rawArma.get(3).toString()));
                arresto.setIdOficial(Integer.parseInt(rawArma.get(4).toString()));
            }
        }
        
        return arrestos;
    }
    
    /**
     * Saves the values of this instance in the database
     * @return true if the operations completes successfully otherwise returns false
     * @throws SQLException if some database exception happends during the excecution of this operation
     * @throws Exception if some exception happends during the excecution of this operation
     */
    public boolean save() throws SQLException, Exception {
        if (getId() > 0) { // this is already save no need to continue
            return true;
        }
        Map<String,Object> values = new LinkedHashMap<String,Object>();
        values.put(FECHA, getFecha());
        values.put(CAUSA, getCausa());
        values.put(HORAS, getHoras());
        values.put(ID_OFICIAL, getIdOficial());
        
        int rowsAffected = queryGenerator.insert(TABLE, values);
        
        ArrayList<Object> data = null;
        if (rowsAffected > 0) {
            AccesoDatos ad = queryGenerator.getOpenConnection();
            data = new ArrayList<Object>(ad.rawQuery("SELECT currval(\'sqarresto\');")); // get the id
        }
        
        if (data != null && data.size() > 0) {
            ArrayList<Object> row = (ArrayList<Object>) data.get(0);
            Double temp = Double.parseDouble(row.get(0).toString());
            this.id = temp.intValue();
            return true;
        }
        return false;
    }
    
    /**
     * Updates the database with the values of this instance
     * @return true if more than 0 rows are affected by this operation
     * @throws SQLException if some exception related with the database happend
     * @throws Exception if some other exception happend during the excecution of this operation happend
     */
     public boolean update() throws SQLException, Exception {
        int rowsAffected;
        Map<String, Object> values = new LinkedHashMap<String, Object>();
        values.put(FECHA, getFecha());
        values.put(CAUSA, getCausa());
        values.put(HORAS, getHoras());
        values.put(ID_OFICIAL, getIdOficial());
        
        Map<String,Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        
        rowsAffected = queryGenerator.update(TABLE, values, conditions);
        
        return rowsAffected > 0;
    }
     
     /**
      * Deletes the entity from the database where it contains the indentifier of this instance
      * @return true if this entity is delete otherwise false
      * @throws SQLException if some database exception happend during the excecution of the operation
      * @throws Exception if some exception happend during the excecution of the operation
      */
     public boolean delete() throws SQLException, Exception {
        if (id <= 0) { // this is already delete
            return true;
        }

        Map<String, Object> conditions = new LinkedHashMap<String, Object>();
        conditions.put(ID, getId());
        int rowsAffected = queryGenerator.delete(TABLE, conditions);

        if (rowsAffected > 0) {
            id = 0;
        }

        return rowsAffected > 0;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @return the fecha
     */
    public Date getFecha() {
        return fecha;
    }

    /**
     * @param fecha the fecha to set
     */
    public void setFecha(Date fecha) {
        this.fecha = fecha;
    }

    /**
     * @return the causa
     */
    public String getCausa() {
        return causa;
    }

    /**
     * @param causa the causa to set
     */
    public void setCausa(String causa) {
        this.causa = causa;
    }

    /**
     * @return the horas
     */
    public int getHoras() {
        return horas;
    }

    /**
     * @param horas the horas to set
     */
    public void setHoras(int horas) {
        this.horas = horas;
    }

    /**
     * @return the idOficial
     */
    public int getIdOficial() {
        return idOficial;
    }

    /**
     * @param idOficial the idOficial to set
     */
    public void setIdOficial(int idOficial) {
        this.idOficial = idOficial;
    }
    
}
